package com.cmj.module_framework;

import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.cmj.basemodule.BaseActivity;
import com.cmj.basemodule.Event.MessageEvent;
import com.cmj.commonmodule.Common;
import com.cmj.homemodule.HomeFragment;
import com.cmj.minemodule.MeFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public class MainActivity extends BaseActivity implements View.OnClickListener {
    Button home;
    Button mine;
    Button other;
    Fragment[] mFragments = new Fragment[3];
    android.support.v4.app.Fragment homeFragment;
    Fragment meFragment;
    Fragment otherFragment;
    private FragmentManager mFragmentManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        home = findViewById(R.id.btn_main);
        mine = findViewById(R.id.btn_mine);
        other = findViewById(R.id.btn_other);
        homeFragment = new HomeFragment();
        meFragment = new MeFragment();
        otherFragment = new OtherFragment();
        mFragments[0] = homeFragment;
        mFragments[1] = meFragment;
        mFragments[2] = otherFragment;
        mFragmentManager = getSupportFragmentManager();
        home.setOnClickListener(this);
        mine.setOnClickListener(this);
        other.setOnClickListener(this);
        setTab(0);

        Common.get();
        Common.getService2();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setTab(0);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    private void setTab(int i) {
       FragmentTransaction transaction = mFragmentManager.beginTransaction();
        hideAllFragments(transaction);
        if (mFragments[i].isAdded()) {
            transaction.show(mFragments[i]);
        } else {
            transaction.add(R.id.frame_layout, mFragments[i]);
            transaction.show(mFragments[i]);
        }
        transaction.commit();


    }

    private void hideAllFragments(FragmentTransaction transaction) {
        for (int i = 0; i < mFragments.length; i++) {
            transaction.hide(mFragments[i]);
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.btn_main:
                setTab(0);
                break;
            case R.id.btn_mine:
                setTab(1);
                Common.common();
                break;
            case R.id.btn_other:
                setTab(2);
                break;
        }

    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        Toast.makeText(this, event.mes, Toast.LENGTH_SHORT).show();
    }
}
